<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>promise1</title>
</head>

<body>
    <script>
        //1.为了解决回调地狱问题
        //2.promise 承诺 保证 (封装promise) (调用promise对象的结果)
        function ajax(option) {
            let type = option.type || "get";
            let url = option.url;
            let data = option.name || "";
            let dataType = option.dataType || "json";
            let success = option.success;
            let xhr = new XMLHttpRequest();
            if (type === "get") {
                xhr.open("get", url + "?" + data);
                xhr.send();
            } else {
                xhr.open("post", url);
                xhr.setRequestHeader("Content-Type", "x-www-form-urlencoded");
                xhr.send(data);
            }
            xhr.responseType = dataType;
            xhr.onload = () => {
                success(xhr.response);
            }
        }

        // ajax({
        //     url:"https://mock.mengxuegu.com/mock/6673cc1fbe17764121dfc577/ajax/phone",
        //     data:"phone=13112345678",
        //     success(res){
        //         console.log(res)
        //     }
        // });
        // ajax({
        //     type:"post",
        //     url:"https://mock.mengxuegu.com/mock/6673cc1fbe17764121dfc577/ajax/phone",
        //     data:"phone=13112345678",
        //     success(res){
        //         console.log(res)
        //     }
        // });
        // let p = new Promise(function (resolve, reject) {
        //     ajax({
        //         url: "https://mock.mengxuegu.com/mock/6673cc1fbe17764121dfc577/ajax/student/1",
        //         success(res) {
        //             let cid = res.data.cid;
        //             resolve(cid);
        //         }
        //     });
        // });
        // p.then(res => {
        //     ajax({
        //         url: "https://mock.mengxuegu.com/mock/6673cc1fbe17764121dfc577/ajax/class/" + res,
        //         success(res) {
        //             console.log(res);
        //         }
        //     });
        // });
        //状态：1.pending 等待中
        //     2.fullfilled 成功状态
        //     3.reject 失败状态 
        function p1() {
            return new Promise(function (resolve, reject) {
                ajax({
                    url: "https://mock.mengxuegu.com/mock/6673cc1fbe17764121dfc577/ajax/student/1",
                    success(res) {
                        let cid = res.data.cid;
                        resolve(cid);
                        // reject("error1");
                    }
                });
            });
        }
        function p2(id) {
            return new Promise(function (resolve, reject) {
                ajax({
                    url: "https://mock.mengxuegu.com/mock/6673cc1fbe17764121dfc577/ajax/class/" + id,
                    success(res) {
                        // resolve(res);
                        reject("error2");
                    }
                });
            });
        }
        p1()
        .then((res)=>p2(res))
        .then(res=>{
            console.log(res)
        }).catch((e)=>{
            console.log(e);
        })
       

        
    </script>
</body>

</html>